package com.yang.security.utils;

import com.yang.security.model.LoginUser;
import com.yang.security.token.TokenHelper;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by baipan
 * Date: 2019-12-18
 * User 工具类
 */
public class UserUtils {


    private static final ThreadLocal<String> THREAD_LOCAL_USER_ID = new ThreadLocal<>();
    private static final ThreadLocal<LoginUser> THREAD_LOCAL_USER = new ThreadLocal<>();


    /**
     * 获取请求中token
     */
    public static String getToken(HttpServletRequest request) {
        return TokenHelper.getToken(request);
    }


    /**
     * 设置用户ID
     */
    public static void setUserId(String userId) {
        THREAD_LOCAL_USER_ID.set(userId);
    }


    /**
     * 获取用户ID
     */
    public static String getUserId() {
        return THREAD_LOCAL_USER_ID.get();
    }


    /**
     * 设置用户信息
     */
    public static void setUser(LoginUser user) {
        THREAD_LOCAL_USER.set(user);
    }


    /**
     * 获取用户信息
     */
    public static LoginUser getUser() {
        return THREAD_LOCAL_USER.get();
    }


    /**
     * 清楚全部的用户信息
     */
    public static void clear() {
        THREAD_LOCAL_USER_ID.remove();
        THREAD_LOCAL_USER.remove();
    }


}
